package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.PmsBrand;
import com.itheima.mapper.PmsBrandMapper;
import com.itheima.service.db.PmsBrandService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@DubboService
public class PmsBrandServiceImpl implements PmsBrandService {
    @Autowired
    PmsBrandMapper brandMapper;

    @Override
    public PageBeanVo findByPageAndConddtion(String keyword, Integer pageNum, Integer pageSize) {
        Page<PmsBrand> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsBrand> lqw = new LambdaQueryWrapper<>();
        lqw.like(keyword != null, PmsBrand::getName, keyword);
        Page<PmsBrand> pmsBrandPage = brandMapper.selectPage(page, lqw);
        return new PageBeanVo(pageNum, pageSize, pmsBrandPage.getTotal(), pmsBrandPage.getRecords());
    }

    @Override
    public void save(PmsBrand pmsBrand) {
        brandMapper.insert(pmsBrand);
    }

    @Override
    public void update(PmsBrand brand) {
        brandMapper.updateById(brand);
    }

    @Override
    public List<PmsBrand> getAll() {
        return brandMapper.selectList(null);
    }

    @Override
    public void updateFactoryStatus(Integer[] ids, Integer factoryStatus) {
        LambdaUpdateWrapper<PmsBrand> luw = new LambdaUpdateWrapper<>();
        luw.in(PmsBrand::getId, ids);
        luw.set(PmsBrand::getFactoryStatus, factoryStatus);
        brandMapper.update(null, luw);
    }

    @Override
    public void updateStatus(Integer[] ids, Integer showStatus) {
        LambdaUpdateWrapper<PmsBrand> luw = new LambdaUpdateWrapper<>();
        luw.in(PmsBrand::getId, ids);
        luw.set(PmsBrand::getShowStatus, showStatus);
        brandMapper.update(null, luw);
    }

    @Override
    public PmsBrand findById(Integer id) {
        return brandMapper.selectById(id);
    }

    @Override
    public void deleteById(Long id) {
        brandMapper.deleteById(id);
    }

    @Override
    public void deleteByIds(Long[] ids) {
        brandMapper.deleteBatchIds(Arrays.asList(ids));
    }




    @Autowired
    PmsBrandMapper pmsBrandMapper;
    @Override
    public Page<PmsBrand> getList(String keyword, Integer pageNum, Integer pageSize) {
        Page<PmsBrand> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsBrand> lqw=new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotBlank(keyword),PmsBrand::getName,keyword);

        return pmsBrandMapper.selectPage(page,lqw);
    }
}